from typing import Dict, Any

import requests


class WeatherService:
    """"演示patch的用法
    """
    def __init__(self,api_key: str="demo_key"):
        self.api_key=api_key
        self.base_url="https://api.weather.gov"

    def get_current_weather(self,city: str)->Dict[str,Any]:
        url=f"{self.base_url}/current.json"
        params={
            "key":self.api_key,
            "city":city,
            "api": "no"
        }

        try:
            response=requests.get(url,params=params)
            response.raise_for_status()
            return response.json()
        except requests.RequestException as e:
            return {"error": f"请求失败：{str(e)}"}

    def get_temperature(self,city: str)-> float:
        """获取指定成十的温度"""
        weather_data=self.get_current_weather(city)

        if "error" in weather_data:
            raise ValueError(f"无法获取天气数据：{weather_data['error']}")

        return weather_data["current"]["temp_c"]

    def is_rainy(self,city: str)->bool:
        """判断是否下雨"""
        weather_data=self.get_current_weather(city)
        if "error" in weather_data:
            return False

        condition=weather_data["current"]["condition"]["text"].lower()
        rainy_conditions=["rain","drizzle","shower","storm"]
        return any(rain in condition for rain in rainy_conditions)

    def get_weather_summary(self,city: str)->str:
        weather_data=self.get_current_weather(city)
        if "error" in weather_data:
            return f"无法获取{city}的天气信息"

        temp=weather_data["current"]["temp_c"]
        condition=weather_data["current"]["weather"][0]["main"].lower()

        return "获取到了天气信息"






